﻿Imports System.Data.SqlClient
Public Class frmJueces
    Dim id As Integer
    Dim ad As New AccesoDatos()
    Dim juez As New Juez()
    Dim consulta As New consultas()

    Private Sub frmJueces_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        'TODO: esta línea de código carga datos en la tabla 'TPI_PAVDataSet.juez' Puede moverla o quitarla según sea necesario.
        Me.JuezTableAdapter.Fill(Me.TPI_PAVDataSet.juez)
        Me.id = -1
        habilitaBotones(True)
        habilitaLabels(False)
    End Sub

    Private Sub btnSalir_Click(sender As System.Object, e As System.EventArgs) Handles btnSalir.Click
        Dim Respuesta As Integer = MessageBox.Show("¿Esta seguro que quiere salir?", "Confirmar", MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1)
        If Respuesta = 1 Then
            End
        End If
    End Sub
    Private Sub btnGuardar_Click(sender As System.Object, e As System.EventArgs) Handles btnGuardar.Click

        Dim valida As New Validations
        If (valida.validarNoNull(Me)) Then
            If (valida.validaDatePickers(Me)) Then
                If (valida.validaComboBox(Me)) Then
                    Try
                        juez.idInfo() = Me.id
                        juez.nombreInfo() = Me.txtNombre.Text
                        juez.apellidoInfo() = Me.txtApellido.Text
                        juez.fechaNacInfo() = Me.dtpFechaNac.Text
                        juez.nroDocInfo() = Me.txtNroDoc.Text
                        juez.tipoDocInfo() = Me.cmbTipoDoc.SelectedItem

                        If (Me.id = -1) Then
                            consulta.InsertJuez(juez)
                        Else
                            consulta.ModificarJuez(juez)
                        End If
                        Me.dgwJueces.DataSource = consulta.Consultar("juez")
                        Me.id = -1
                        Me.habilitaTextBox(False)
                        Me.habilitaBotones(True)
                    Catch ex As Exception

                    End Try
                End If
            End If
        End If

       

    End Sub

    Private Sub dgwJueces_CellDoubleClick(sender As Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgwJueces.CellDoubleClick
        Me.id = Me.dgwJueces.Rows(e.RowIndex).Cells(0).Value
        Me.txtNombre.Text = Me.dgwJueces.Rows(e.RowIndex).Cells(1).Value
        Me.txtApellido.Text = Me.dgwJueces.Rows(e.RowIndex).Cells(2).Value
        Me.txtNroDoc.Text = Me.dgwJueces.Rows(e.RowIndex).Cells(3).Value
        Me.cmbTipoDoc.Text = Me.dgwJueces.Rows(e.RowIndex).Cells(4).Value
        Me.dtpFechaNac.Text = Me.dgwJueces.Rows(e.RowIndex).Cells(5).Value
        Me.habilitaTextBox(True)
        Me.habilitaBotones(False)
    End Sub


    Private Sub btn_eliminar_Click(sender As System.Object, e As System.EventArgs) Handles btnEliminar.Click
        consulta.Eliminar("juez", Me.id)
        Me.dgwJueces.DataSource = consulta.Consultar("juez")
        Me.id = -1
        Me.habilitaLabels(False)
        Me.habilitaBotones(True)
    End Sub


    Private Sub btnNuevo_Click(sender As System.Object, e As System.EventArgs) Handles btnNuevo.Click
        Me.id = -1
        Me.habilitaLabels(True)
        Me.habilitaBotones(False)
        Me.btnEliminar.Enabled = False
    End Sub

    Private Sub habilitaBotones(ByVal param As Boolean)
        Me.btnSalir.Enabled = param
        Me.btnNuevo.Enabled = param
        Me.btnGuardar.Enabled = Not (param)
        Me.btnCancelar.Enabled = Not (param)
        Me.btnEliminar.Enabled = Not (param)
    End Sub
    Private Sub habilitaTextBox(ByVal param As Boolean)
        Me.txtNombre.Enabled = param
        Me.txtApellido.Enabled = param
        Me.txtNroDoc.Enabled = param
        Me.cmbTipoDoc.Enabled = param
        Me.dtpFechaNac.Enabled = param
    End Sub
    Public Sub habilitaLabels(ByVal param As Boolean)
        Me.habilitaTextbox(param)

        Me.txtNombre.Text = ""
        Me.txtApellido.Text = ""
        Me.txtNroDoc.Text = ""
        Me.cmbTipoDoc.Text = ""
        Me.dtpFechaNac.Text = ""
    End Sub

    Private Sub btnCancelar_Click(sender As System.Object, e As System.EventArgs) Handles btnCancelar.Click
        Me.habilitaLabels(False)
        Me.habilitaBotones(True)
    End Sub
End Class